home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Samples / C++ / Direct3D / MeshFromOBJ / MeshLoader.h < prev    next >
Encoding:
C/C++ Source or Header  |  2004-09-27  |  2.5 KB  |  89 lines

  1. //--------------------------------------------------------------------------------------
  2. // File: MeshLoader.h
  3. //
  4. // Wrapper class for ID3DXMesh interface. Handles loading mesh data from an .obj file
  5. // and resource management for material textures.
  6. //
  7. // Copyright (c) Microsoft Corporation. All rights reserved.
  8. //--------------------------------------------------------------------------------------
  9. #ifndef _MESHLOADER_H_
  10. #define _MESHLOADER_H_
  11. #pragma once
  12.  
  13. // Vertex format
  14. struct VERTEX
  15. {
  16.     D3DXVECTOR3 position;
  17.     D3DXVECTOR3 normal;
  18.     D3DXVECTOR2 texcoord;
  19. };
  20.  
  21.  
  22. // Used for a hashtable vertex cache when creating the mesh from a .obj file
  23. struct CacheEntry
  24. {
  25.     UINT index;
  26.     CacheEntry* pNext;
  27. };
  28.  
  29.  
  30. // Material properties per mesh subset
  31. struct Material
  32. {
  33.     WCHAR strName[MAX_PATH];
  34.  
  35.     D3DXVECTOR3 vAmbient;
  36.     D3DXVECTOR3 vDiffuse;
  37.     D3DXVECTOR3 vSpecular;
  38.  
  39.     int nShininess;
  40.     float fAlpha;
  41.  
  42.     bool bSpecular;
  43.  
  44.     WCHAR strTexture[MAX_PATH];
  45.     IDirect3DTexture9* pTexture;
  46.     D3DXHANDLE hTechnique;
  47. };
  48.  
  49.  
  50. class CMeshLoader
  51. {
  52. public:
  53.     CMeshLoader();
  54.     ~CMeshLoader();
  55.  
  56.     HRESULT Create( IDirect3DDevice9* pd3dDevice, const WCHAR* strFilename );
  57.     void    Destroy();
  58.     
  59.     
  60.     UINT GetNumMaterials() const { return m_Materials.GetSize(); }
  61.     Material* GetMaterial( UINT iMaterial ) { return m_Materials.GetAt( iMaterial ); }
  62.  
  63.     ID3DXMesh* GetMesh() { return m_pMesh; }
  64.     WCHAR* GetMediaDirectory() { return m_strMediaDir; }
  65.  
  66. private:
  67.     
  68.     HRESULT LoadGeometryFromOBJ( const WCHAR* strFilename );
  69.     HRESULT LoadMaterialsFromMTL( const WCHAR* strFileName ); 
  70.     void    InitMaterial( Material* pMaterial );
  71.     
  72.     DWORD   AddVertex( UINT hash, VERTEX* pVertex );
  73.     void    DeleteCache();
  74.  
  75.     IDirect3DDevice9* m_pd3dDevice;    // Direct3D Device object associated with this mesh
  76.     ID3DXMesh*        m_pMesh;         // Encapsulated D3DX Mesh
  77.  
  78.     CGrowableArray< CacheEntry* > m_VertexCache;   // Hashtable cache for locating duplicate vertices
  79.     CGrowableArray< VERTEX >      m_Vertices;      // Filled and copied to the vertex buffer
  80.     CGrowableArray< DWORD >       m_Indices;       // Filled and copied to the index buffer
  81.     CGrowableArray< DWORD >       m_Attributes;    // Filled and copied to the attribute buffer
  82.     CGrowableArray< Material* >   m_Materials;     // Holds material properties per subset
  83.  
  84.     WCHAR m_strMediaDir[ MAX_PATH ];               // Directory where the mesh was found
  85. };
  86.  
  87. #endif // _MESHLOADER_H_
  88.  
  89.